package chaw.android.train;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import chaw.android.util.HttpRequestManager;
import chaw.android.util.Preferences;
import chaw.android.util.ServerURL;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class ReplyWriteActivity extends Activity {
	private ImageView back, cancel;
	private String content, user_id, board_id, station_name, station_id;
	private EditText text;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.reply_write);
        back = (ImageView)findViewById(R.id.back);
        cancel = (ImageView)findViewById(R.id.cancel);
        new Handler().postDelayed(new Runnable(){
        	@Override
        	public void run() {
        		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        		imm.showSoftInput(findViewById(R.id.title), 0);  
        	}
    
        }, 100);
        setClickListener();
	}

	private class UploadAsynkTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			// TODO Auto-generated method stub
			String response = null;
			
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			
			if(urls.length > 1)
				for(int i = 1; i< urls.length; i+=2)
					params.add(new BasicNameValuePair(urls[i], urls[i+1]));
			response = HttpRequestManager.request(urls[0], params);
			return response;
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			try {
				JSONObject resultData = new JSONObject(result);
				if(resultData.getString("result").equals("success")){
					finish();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void setClickListener(){
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				text = (EditText)findViewById(R.id.content);
				content = text.getText().toString();
				UploadAsynkTask task = new UploadAsynkTask();
				Intent intent = getIntent();
				user_id = Preferences.getValue(getBaseContext(), "user_id");
				board_id = intent.getExtras().get("board_id").toString();
				station_name = intent.getExtras().get("station_name").toString();
				station_id = intent.getExtras().get("station_id").toString();

				String message = Preferences.getValue(getBaseContext(), "nick_name") + "님이 " + station_name + "역 이야기에 댓글을 남겼습니다.";
				
				String[] params = new String[]{ ServerURL.addReply, "content", content, "user_id", user_id, 
												"board_id", board_id, "message", message.toString(), "station_id", station_id };
				if(content.length() > 0){
					task.execute(params);	
				}
				else{
					Toast.makeText(getBaseContext(), "내용은 최소 한 글자 이상이여야 합니다.", Toast.LENGTH_SHORT).show();
				}
			}
		});
		cancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
}
